package cn.itcast.bos.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.utils.PinYin4jUtils;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("propotype")
public class AreaAction extends BaseAction<Area>{
	
	private File file;

	public void setFile(File file) {
		this.file = file;
	}
	
	@Autowired
	private AreaService areaService;
	
	@Action(value="areaImport")
	public String areaImport() throws Exception{
		ArrayList<Area> list = new ArrayList<Area>();
		HSSFWorkbook hss = new HSSFWorkbook(new FileInputStream(file));
		
		HSSFSheet sheet = hss.getSheetAt(0);
		
		for (Row row : sheet) {

			if (row.getRowNum()==0) {
				continue;
			}
			if (row.getCell(0)==null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			String province = row.getCell(1).getStringCellValue();
			area.setProvince(province);
			String city = row.getCell(2).getStringCellValue();
			area.setCity(city);
			String district = row.getCell(3).getStringCellValue();
			area.setDistrict(district);
			area.setPostcode(row.getCell(4).getStringCellValue());
			String[] str = PinYin4jUtils.getHeadByString(province+city+district);
			StringBuffer buffer = new StringBuffer();
			for (String string : str) {
				buffer.append(string);
			}
			String shortcode = buffer.toString();
			area.setShortcode(shortcode);
			String citycode = PinYin4jUtils.hanziToPinyin(city,"");
			area.setCitycode(citycode);
			list.add(area);
		}
		areaService.saveBath(list);
		return NONE;
	}
	
	
	@Action(value="pageSearch",results={@Result(name="success",type="json")})
	public String pageSearch(){
		Pageable pageable = new PageRequest(page-1, rows);
		Specification<Area> specification = new Specification<Area>() {

			@Override
			public Predicate toPredicate(Root<Area> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				ArrayList<Predicate> list = new ArrayList<Predicate>();
				
					if (StringUtils.isNotBlank(model.getProvince())) {
						Predicate p1 = cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
						list.add(p1);
					}
					if (StringUtils.isNotBlank(model.getCity())) {
						Predicate p2 = cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
						list.add(p2);
					}
					if (StringUtils.isNotBlank(model.getDistrict())) {
						Predicate p3 = cb.like(root.get("district").as(String.class),"%"+model.getDistrict()+"%");
						list.add(p3);
					}
				
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		Page<Area> page = areaService.pageSearch(pageable,specification);
		pushPageDataToValueStack(page);
		return SUCCESS;
	}
}
